from  ReadReview import Read
from getCommodity import GetCommodity
from datetime import *
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import math



                  
def getDateFromTime(time):
     year = int(time['year'])
     month = int(time['month'])
     day = int(time['day'])
     D = date(year, month, day)
     return D.weekday()  # return 0 if monday;  1 if tuesday and so on

list = []
categorys=[]
numOfEachCategory = []
Records=[  [],[],[],[],[],[],[]  ]   
numOfReviews=[0,0,0,0,0,0,0]

def addNumOfCategory(c,weekday):
    if(c in categorys):
        numOfEachCategory[categorys.index(c)][weekday]=numOfEachCategory[categorys.index(c)][weekday]+1
    else:
        categorys.append(c)
        numOfEachCategory.append([0,0,0,0,0,0,0])
        numOfEachCategory[len(numOfEachCategory)-1][weekday]=numOfEachCategory[len(numOfEachCategory)-1][weekday]+1


def getCV(numOfACategory):
    #cv is bianyixishu,  Coefficient of Variation
    sum=0
    for i in range(0,len(numOfACategory)):
        sum=sum+numOfACategory[i]
    average=sum/len(numOfACategory)  
    x=0
    for i in range(0,len(numOfACategory)):
         x=x+(numOfACategory[i]-average)*(numOfACategory[i]-average)
    x=x/len((numOfACategory))
    x=math.sqrt(x)     # biaozhuncha 
    if(average>0):
      return x/average
    else:
      return 0
    

def getRecommendCategory():
    #for each category,get the days it may be recommended
    
    # first,   sort them
 
    for i in range(0,len(numOfEachCategory)):
        max=0
        # get the max
        for j in range(0,len(numOfEachCategory[i])):
                if(numOfEachCategory[i][j]>max):
                    max=numOfEachCategory[i][j]        
        if(getCV(numOfEachCategory[i])>0.1):
             for j in range(0,len(numOfEachCategory[i])):
                 if(numOfEachCategory[i][j]>=max*0.99):
                   Records[j].append({'category':categorys[i],'number':numOfEachCategory[i][j]})
    
    
    # sort them
    for x in range(0,len(Records)):
        for i in range(0,len(Records[x])-1):
            for j in range(0,len(Records[x])-1):
              if(Records[x][j]['number']<Records[x][j+1]['number']):
                        temp=Records[x][j]
                        Records[x][j]=Records[x][j+1]
                        Records[x][j+1]=temp
    get=GetCommodity()
    for x in range(0,len(Records)):
        print('day' +str(x+1))
        print(get.getByCategory(list, Records[x][0]['category']))
        print(get.getByCategory(list, Records[x][1]['category']))
        print(get.getByCategory(list, Records[x][2]['category']))
        
                      

def  draw():
    days = (1,2,3,4,5,6,7)
    maxs=(Records[0][0]['number'],Records[1][0]['number'],Records[2][0]['number'],Records[3][0]['number'],
          Records[4][0]['number'],Records[5][0]['number'],Records[6][0]['number'],)
    plt.plot(days, maxs)
    plt.title('Max Number Of Reviews Of Each  Weekday')
    plt.xlabel('weekdays')
    plt.ylabel('maxNumber')
    print(maxs)
    plt.show()


if __name__ == '__main__':
   info = Read()
   list = info.get_infolist()
   print('complete reading file,start to analyse')
   for i in range(0, len(list)):
        category = list[i]['category']
        weekday = getDateFromTime(list[i]['time'])
        numOfReviews[weekday]=numOfReviews[weekday]+1
        addNumOfCategory(category,weekday)
   # end  fenleiwancheng
   print('start to recommend')
   getRecommendCategory()
   draw()
   
        
        
